define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vppz/vip_code/index' + location.search,
                    add_url: 'vppz/vip_code/add',
                    download_url: 'vppz/vip_code/download',
                    downloadqrcode_url: 'vppz/vip_code/downloadqrcode',
                    detail_url: 'vppz/vip_card/index',
                    del_url: 'vppz/vip_code/del',
                    table: 'vppz_vip_card_package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //fixedColumns: true,
                //fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
						{field: 'card_num', title: '会员码数量', operate: 'LIKE',searchable:false},
						{field: 'vip_package_title', title: '会员卡类型', operate: 'LIKE',searchable:false},
						{field: 'image_url', title: '卡片底图',events: Table.api.events.image,formatter: Table.api.formatter.image,searchable:false},

                        {field: 'admin.username', title: '制作人', operate:'BETWEEN',searchable:false},
						{field: 'createtime', title: '制作时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [{
                                    name: 'godownload',
                                    title: '下载',
									text: '下载',
                                    classname: 'btn btn-xs btn-success btn-download',
                                    icon: 'fa fa-list',
                                    url: 'vppz/vip_code/download',
                                // 添加点击事件处理
                                    click: function (data, row, index, btn) {
                                        console.log(row);
                                        // 直接下载而不打开对话框
                                        window.location.href = 'vppz/vip_code/download?ids=' + row.id;
                                        return false; // 阻止默认行为
                                    },
                                    success:function (){
                                        Layer.closeAll();
                                    },
                                    callback: function (data) {
                                        // 下载完成后可以执行的操作
                                        Layer.closeAll();
                                    }
                                },{
                                    name: 'godownloadqrcode',
                                    title: '下载二维码',
									text: '下载二维码',
                                    classname: 'btn btn-xs btn-success btn-downloadqrcode',
                                    icon: 'fa fa-list',
                                    url: 'vppz/vip_code/downloadqrcode',
                                // 添加点击事件处理
                                    click: function (data, row, index, btn) {
                                        console.log(row);
                                        // 直接下载而不打开对话框
                                        window.location.href = 'vppz/vip_code/downloadqrcode?ids=' + row.id;
                                        return false; // 阻止默认行为
                                    },
                                    success:function (){
                                        Layer.closeAll();
                                    },
                                    callback: function (data) {
                                        // 下载完成后可以执行的操作
                                        Layer.closeAll();
                                    }
                                },{
                                    name: 'detail',
                                    title: '明细',
									text: '明细',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'vppz/vip_card/index',
                                }]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            // 添加自定义事件处理 - 监听下载按钮点击
            $(document).on('click', '.btn-download', function(e) {
                e.preventDefault();
                e.stopPropagation();

                // 获取当前行的数据
                var $tr = $(this).closest('tr');
                var index = $tr.data('index');
                var rowData = table.bootstrapTable('getData')[index];
                var loading_img = layer.load(1, {
                    shade: [1,'#ccc'] //0.1透明度的白色背景
                });
                if (rowData && rowData.id) {
                    // 直接触发下载
                    window.location.href = '/admin888.php/vppz/vip_code/download?ids=' + rowData.id;
                    // 关闭所有弹窗
                    //数据调取完毕时使用
                    setTimeout(function (){
                        layer.close(loading_img);
                    },500);
                    Layer.closeAll();
                }
                return false;
            });
            $(document).on('click', '.btn-downloadqrcode', function(e) {
                e.preventDefault();
                e.stopPropagation();

                // 获取当前行的数据
                var $tr = $(this).closest('tr');
                var index = $tr.data('index');
                var rowData = table.bootstrapTable('getData')[index];
                var loading_img = layer.load(1, {
                    shade: [1,'#ccc'] //0.1透明度的白色背景
                });
                if (rowData && rowData.id) {
                    // 直接触发下载
                    window.location.href = '/admin888.php/vppz/vip_code/downloadqrcode?ids=' + rowData.id;
                    // 关闭所有弹窗
                    //数据调取完毕时使用
                    setTimeout(function (){
                        layer.close(loading_img);
                    },500);
                    Layer.closeAll();
                }
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },

        download: function () {
            // 下载操作完成后关闭弹窗
            console.log('asdfasdf');
            if (typeof Layer !== 'undefined') {
                Layer.closeAll();
            }
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
